package de.medizin.uni.halle.irm;

import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

import com.actionbarsherlock.app.SherlockFragment;

public class SendLetterFragment extends SherlockFragment {
	private ArrayAdapter<String> listAdapter; 
	SQLiteDatabase database;
	Cursor c;
	Bundle bundle;
	private String emailcontent, emailsubject;
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
    	database = getSherlockActivity().openOrCreateDatabase("born", Context.MODE_PRIVATE, null);
		View rm = (LinearLayout)inflater.inflate(R.layout.send, container, false);  
		Button print = (Button) rm.findViewById(R.id.send_button);
		print.setText("Drucken");
		bundle = savedInstanceState;
		
		updateLetterList(rm);
	    return rm;	        
	}
	private void updateLetterList(final View v) {
		ListView lv  = (ListView) v.findViewById(R.id.s_listview);
    	int[] to = new int[] { R.id.ID_CELL, R.id.ADDRESS_CELL, R.id.LASTNAME_CELL,R.id.FIRSTNAME_CELL,R.id.DATE_CELL };
        String[] fields = new String[] {"sid","email","lastname", "firstname", "date"};
    	c = database.rawQuery("Select p._id, p.sid, p.email, p.phone, p.firstname, p.lastname, p.rank, pl.date from patients p, plans pl where pl.contacttype=4 and p._id=pl.patient_id and julianday('now') >= julianday(pl.date) order by date;" , null);
    	if(c.getCount()>0){
	    	CursorAdapter data = new SimpleCursorAdapter(getSherlockActivity(), R.layout.mail_row, c, fields, to, 0 );
	    	lv.setAdapter(data);  
	    	((Button) v.findViewById(R.id.send_button)).setVisibility(View.VISIBLE);
	    	((Button) v.findViewById(R.id.send_button)).setOnClickListener(new OnClickListener(){
				public void onClick(View arg0) {
					print_letters(v);					
				}	    		
	    	});
	    	((ListView) v.findViewById(R.id.s_listview)).setOverScrollMode(View.OVER_SCROLL_IF_CONTENT_SCROLLS);
	    	setonclick(lv, v);
    	}
    	else {
    		String[] mails = new String[] {"Es sind keine Briefe zu verschicken"}; 
            listAdapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item_overview, mails);  
            lv.setAdapter( listAdapter ); 
            ((ListView) v.findViewById(R.id.s_listview)).setOverScrollMode(View.OVER_SCROLL_NEVER);
            ((Button) v.findViewById(R.id.send_button)).setVisibility(View.INVISIBLE);
    	}	
	}
	private void setonclick(ListView lv, View v) {
		// TODO Auto-generated method stub
		
	}
	protected void print_letters(View v) {
		// TODO Auto-generated method stub
		
	}
}